define(
["./BasicRenderSyst", "framework/graphic/WebGL"],
function(BasicRenderSyst, WebGL){
    return {
        create: function() {

            var _self = {

                init: function(systemMgr, posBuf, texBuf, indBuf, norBuf, texture) {
                    //Get and register myself to the system
                    _system = systemMgr.getSystem(BasicRenderSyst);
                    _system.add(_self);

                    //Assign buffers
                    _posBuf = posBuf;
                    _texBuf = texBuf;
                    _indBuf = indBuf;
                    _norBuf = norBuf;
                    _texture= texture;

                    return _self;
                },

                destroy: function() {
                    //Unregister myself from the system
                    _system.rem(_self);
                },

                setPolyOffset: function(active, val_0, val_1) {
                    _isPolyOffsetActive = active;
                    if(val_0) _polygonOffset[0] = val_0;
                    if(val_1) _polygonOffset[1] = val_1;
                },

                /**
                 * Call this to draw to the screen
                 */
                render: function(shader) {
                    //Assign the buffers
                    WebGL.pointAttrib(shader.getAttribute("aPos"), _posBuf);
                    WebGL.pointAttrib(shader.getAttribute("aTex"), _texBuf);
                    WebGL.pointAttrib(shader.getAttribute("aNor"), _norBuf);
                    
                    //Get gl
                    var gl = WebGL.gl;

                    //Activate texture
                    _texture.uploadToGL();
                    gl.activeTexture(gl.TEXTURE0);
                    gl.bindTexture(gl.TEXTURE_2D, _texture.getGLTex());
                    gl.uniform1i(shader.getUniform("sampler_0"), 0);

                    //Polygon offset
                    if(_isPolyOffsetActive) {
                        gl.enable(gl.POLYGON_OFFSET_FILL);
                        gl.polygonOffset(_polygonOffset[0], _polygonOffset[1]);
                    }

                    //Draw call
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indBuf);
                    gl.drawElements(gl.TRIANGLES, _indBuf.numItems, gl.UNSIGNED_SHORT, 0);
                    
                    //Polygon offset
                    if(_isPolyOffsetActive) {
                        gl.disable(gl.POLYGON_OFFSET_FILL);
                    }
                }
            };

            //Privates
            var _polygonOffset = [0,0];
            var _isPolyOffsetActive;
            var _system;
            var _texture;
            var _posBuf;
            var _texBuf;
            var _indBuf;
            var _norBuf;

            return _self;
        }
    }
});